package com.xh.lesson.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.xh.lesson.entity.SysFun;
import com.xh.lesson.entity.SysHouse;
import com.xh.lesson.entity.SysHouseFun;
import com.xh.lesson.exception.BusinessException;
import com.xh.lesson.exception.code.BaseResponseCode;
import com.xh.lesson.mapper.SysFundMapper;
import com.xh.lesson.mapper.SysHouseFundMapper;
import com.xh.lesson.mapper.SysHousedMapper;
import com.xh.lesson.service.SysHouseService;
import com.xh.lesson.utils.PageUtils;
import com.xh.lesson.vo.req.HouseAddReqVo;
import com.xh.lesson.vo.req.HousePageReqVo;
import com.xh.lesson.vo.req.HouseUpdateReqVo;
import com.xh.lesson.vo.resp.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 柴&许
 * @since 2021-07-01
 */
@Service
@Slf4j
public class SysHouseServiceImpl extends ServiceImpl<SysHousedMapper, SysHouse> implements SysHouseService {
    @Resource
    private SysHouseFundMapper houseFundMapper;
    @Resource
    private SysFundMapper sysFundMapper;
    @Resource
    private SysHousedMapper sysHousedMapper;

    @Override
    public SysHouse addHouse(HouseAddReqVo vo) {
        SysHouse sysHouse = new SysHouse();
        sysHouse.setLevel(vo.getLevel());
        sysHouse.setHousename(vo.getHousename());
        sysHouse.setFloorspace(vo.getFloorspace());
        sysHouse.setArea(vo.getArea());
        int insert2 = baseMapper.insert(sysHouse);
        if (insert2 == 0) {
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        SysFun sysFun = new SysFun();
        int insert1 = sysFundMapper.insert(sysFun);
        if (insert1 == 0) {
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        Integer funid = sysFun.getFunid();
        Integer houserid = sysHouse.getHouserid();
        SysHouseFun sysHouseFun = new SysHouseFun();
        sysHouseFun.setHouserid(houserid);
        sysHouseFun.setFunid(funid);
        int insert = houseFundMapper.insert(sysHouseFun);
        if (insert == 0) {
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        return sysHouse;

    }

    @Override
    public void updateHouse(HouseUpdateReqVo vo) {

        SysHouse house = baseMapper.selectById(vo.getHouserid());

        if (house == null) {
            log.error("传入 的 id:{}不合法", vo.getHouserid());
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        SysHouse update = new SysHouse();
        BeanUtils.copyProperties(vo, update);
        int count = baseMapper.updateById(update);
        if (count != 1) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
    }

    @Override
    public void deleted(Integer id) {
        SysHouse house = baseMapper.selectById(id);
        if (null == house) {
            log.error("房屋不存在", id);
            throw new BusinessException(BaseResponseCode.HOUSE_NOT_CHECK);
        }
        int count = sysHousedMapper.deleteById(id);
        if (count != 1) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
    }

    @Override
    public SysHouse selectById(Integer id) {
        SysHouse sysHouse = baseMapper.selectById(id);
        return sysHouse;
    }

    @Override
    public PageVO<SysHouse> pageInfo(HousePageReqVo vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<SysHouse> list = baseMapper.selectAll(vo);
        return PageUtils.getPageVO(list);
    }

    @Override
    public List<SysHouse> selectAll() {
        List<SysHouse> list = baseMapper.selectList(null);
        return list;
    }

    @Override
    public SysHouse soloAddHouse(HouseAddReqVo vo) {

        QueryWrapper<SysHouse> wrapper = new QueryWrapper<>();
        wrapper.eq("housename",vo.getHousename());
        SysHouse sysHouse1 = baseMapper.selectOne(wrapper);
        if (sysHouse1==null){
            SysHouse sysHouse = new SysHouse();
            BeanUtils.copyProperties(vo, sysHouse);
            baseMapper.insert(sysHouse);
            return sysHouse;
        }else {
            return sysHouse1;
        }
    }
}
